<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-05-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';
class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$keyword = isset($this->request['keyword']) ? $this->request['keyword'] : '';
				
		$query_link = "manage.php?m=tips";
		$query_link .= $keyword ? '&keyword='.urlencode($keyword) : '';
		$query_link = $query_link;
				
		$result = get_tips_list($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		
		foreach($list as &$temp)
		{
			$temp['desc'] = cut_string($temp['desc'], 50);
		}
		
		include template('admin/tips');
	}
	
	private function add()
	{
		$info = array();
		if(isset($this->request['c']))
		{
			$info = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$info['create_date'] = isset($info['create_date']) ? $info['create_date'] : date('Y-m-d');
		$info['title'] = isset($this->request['title']) ? $this->request['title'] : '';
		$info['desc'] = isset($info['desc']) ? $info['desc'] : '';
		
		$action = 'do_add';
		$action_name = '添加攻略';
		include template('admin/tips_info');		
	}
	
	private function doAdd()
	{
		//DBFunction::getInstance()->setDebug();
		
		$info['create_date'] = isset($this->request['create_date']) ? strtotime($this->request['create_date']) : date();
		
		$info['title'] = isset($this->request['title']) ? $this->request['title'] : '';
		$info['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		$info['uid'] = $this->user['id'];
		
		if(strlen($info['title']) > 995)
		{
			$info['title'] = cut_string($info['title'], 995);
		}
		
		$result = add_tips($info);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=tips&a=modify&id='.$result);
		}
		else
		{
			$this->messager("添加失败", 'manage.php?m=tips&a=add');
		}
	}
	
	private function modify()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=tips');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{	
			$info = get_tips_info(array('id'=>$id));
			
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("攻略不存在", 'manage.php?m=tips');
			}
		}
		
		$info['create_date'] = date('Y-m-d', $info['create_date']);
		
		$action = 'do_modify&id='.$id;
		$action_name = '编辑攻略';
		include template('admin/tips_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=business');
		}
		
		$tips = get_tips_info(array('id'=>$id));
		
		if(!isset($tips['id']) || $tips['id'] < 1)
		{
			$this->messager("攻略不存在", 'manage.php?m=tips');
		}
		
		$info['create_date'] = isset($this->request['create_date']) ? strtotime($this->request['create_date']) : date();
		
		$info['title'] = isset($this->request['title']) ? $this->request['title'] : '';
		$info['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		$info['id'] = $id;
		$info['uid'] = $this->user['id'];
		
		if(strlen($info['title']) > 995)
		{
			$info['title'] = cut_string($info['title'], 995);
		}
		
		$result = update_tips($info);
		
		if($result)
		{
			$this->messager("修改成功", 'manage.php?m=tips');
		}
		else
		{
			$this->messager("修改失败", 'manage.php?m=tips');
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
				
		if(!$ids)
		{
			$this->messager("请指定ID", 'manage.php?m=tips');
		}
				
		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $id)
			{
				$result = delete_tips($id);
			}
		}
		else
		{
			$result = delete_tips($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=tips');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=tips');
		}
	}
}